package Compare;

import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;

class Student implements Comparable<Student> {
    public int age;
    public String name;


    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

//    @Override
//    public int compareTo(Student o) {
//        return this.age - o.age;
//    }

    @Override
    //默认是小根堆，这样反写变成大根堆
    public int compareTo(Student o) {
        return o.age - this.age;
    }


//    public int  compareTo(Student o) {
//        return this.name.compareTo(o.name);
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }
}

class AgeComparator implements Comparator<Student> {


//    @Override
//    默认小根堆
//    public int compare(Student o1, Student o2) {
//        return o1.age = o2.age;
//    }

    @Override
    //变成了大根堆
    public int compare(Student o1, Student o2) {
        return o2.age = o1.age;
    }
}

class NameComparator implements Comparator<Student> {


//    @Override
//    默认小根堆
//    public int compare(Student o1, Student o2) {
//        return o1.name.compareTo(o2.name);
//    }

    @Override
    //变成了大根堆
    public int compare(Student o1, Student o2) {
        return o2.name.compareTo(o1.name);
    }

}

public class Test {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "ahangsan";
        s1.age = 20;
        Student s2 = new Student();
        s2.name = "zhangsan";
        s2.age = 10;

        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();


        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(nameComparator);
        priorityQueue.offer(s1);
        priorityQueue.offer(s2);

        System.out.println(priorityQueue.poll());
    }

    public static void main4(String[] args) {
        Student s1 = new Student();
        s1.name = "zhangsan";
        s1.age = 20;
        Student s2 = new Student();
        s2.name = "zhangsan";
        s2.age = 10;

        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(s1,s2));

        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(s1,s2));
    }

    public static void main3(String[] args) {
        Student student1 = new Student();
        student1.name = "zhangsan";
        student1.age = 20;
        Student student2 = new Student();
        student2.name = "zhangsan";
        student2.age = 10;
        //此时需要重写equals方法:equals只是比较是否相等，返回值是ture或者false
        System.out.println(student1 == student2);//f
        System.out.println(student1.equals(student2));//t
        System.out.println(student1.compareTo(student2));//10
    }
    public static void main2(String[] args) {
//        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
//
//        List<Integer> list = new ArrayList<>();
//        list.add(1);
//        list.add(2);
//        //默认分配了一个长度为11的queue数组
//        Queue<Integer> priorityQueue1 = new PriorityQueue<>(list);
//        priorityQueue1.offer(11);
//        priorityQueue1.offer(22);
//        priorityQueue1.offer(33);
//        System.out.println(priorityQueue1.poll());
//        System.out.println(priorityQueue1.poll());
//        System.out.println(priorityQueue1.poll());

        Queue<Student> priorityQueue2 = new PriorityQueue<>();
        priorityQueue2.offer(new Student());
        priorityQueue2.offer(new Student());
    }
}
